const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    _ruleing = false;
    _qiandaoing = false;
    
    loadPrefabNode(path, cb) {
        console.log("loadPrefabNode 加载资源");
        cc.loader.loadRes("prefabs/" + path, function (err, res) {
            if(err) {
                console.error(err);
                return;
            }

            let node = cc.instantiate(res);
            if (node == undefined) {
                console.error('loadPrefab undefined path = '+path);
            } else {
                cb && cb(node);
            }
        });
        console.log("loadPrefabNode 加载完成");
    }

    show_rule(data){              
        let game_rule = cc.find("Canvas").getChildByName('game_rule_utils');
        
        if (game_rule != undefined) {            
            game_rule.active = true; 
            let script = game_rule.getComponent("game_rule_ts");
            script.show_label(data);            
        } 
        else {            
            if (this._ruleing) {
                return;
            }            
            this._ruleing = true;
            this.loadPrefabNode('public/game_rule/game_rule_utils', function (game_rule) {
                console.log('加载');
                cc.find("Canvas").addChild(game_rule, 100, 'game_rule_utils');
                let script = game_rule.getComponent("game_rule_ts");
                script.show_label(data);             
                this._ruleing = false;
            }.bind(this));
        }
    }

    show_qiandao(){
        let qiandao = cc.find("Canvas").getChildByName('qian_dao_utils');
        
        if (qiandao != undefined) {            
            qiandao.active = true;
        } 
        else {            
            if (this._qiandaoing) {
                return;
            }            
            this._qiandaoing = true;
            this.loadPrefabNode('public/qian_dao/qian_dao_utils', function (qiandao) {
                console.log('加载');
                cc.find("Canvas").addChild(qiandao, 100, 'qian_dao_utils');                     
                this._qiandaoing = false;
            }.bind(this));
        }
    }

    /**
     * 随机一个数字
     * @param min 从min开始
     * @param max 到max结束，不包括max
     * @returns {*}
     */
    getRandom(min, max) {
        if (isNaN(min) || isNaN(max))return 0;
        if (min >= max) return min;
        var temp = (Math.random() * (max - min + 1) ) + min ;
        temp = Math.floor(temp);
        if(temp == max +1)
        {
            return max;
        }
        else
        {
            return temp;
        }
    }
}
